from datetime import datetime
from app.models.mongo_model import mongo_conn

mon = mongo_conn()  # 连接mongodb
collection = mon['threats']  # 指定集合(web)
collection2 = mon['configurate']  # 指定集合(配置文件)
collection3 = mon['system']  # 指定集合(木马文件、系统执行计划指令)
collection4 = mon['cpu']  # 指定集合(木马文件、系统执行计划指令)
collection5 = mon['corn_jobs']  # 指定集合(木马文件、系统执行计划指令)


# 公共部分(web)
def comm(details=None, ip=None, ua=None, lv=None, path=None):
    pub_info = {
        "details": details,  # 描述
        "ip": ip,
        "ua": ua,  # user-agent
        "Level": lv,  # 威胁程度
        "path": path,  # url路径
        "time": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    }
    collection.insert_one(pub_info)  # 写入mongodb
    print(pub_info, "警告信息已入数据库")


# 公共部分（配置文件）
def config_comm(details, lv, para=None, static=None, suggest=None):
    info = {
        "details": details,
        "Level": lv,
        "para": para,  # 检查的函数
        "static": static,
        "suggest": suggest,
        "time": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    }
    collection2.insert_one(info)  # 写入mongodb
    print(info, "警告信息已入数据库")


# 木马文件检测、系统执行计划指令
def torjan_crontab_info(details, lv, word=None, path=None):
    check_info = {
        "details": details,
        "Level": lv,
        "grammar": word,  # 检查的语法
        "path": path,
        "time": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    }
    collection3.insert_one(check_info)  # 写入mongodb
    print(check_info, "警告信息已入数据库")


# 占用cpu最高的程序
def used_cpu_high(user, pid, cpu, men, command):
    info = {
        "user": user,
        "pid": pid,
        "cpu": cpu,
        "men": men,
        "command": command,
        "time": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    }
    collection4.insert_one(info)  # 写入mongodb
    print(info, "警告信息已入数据库")


# 检测CronJob防止提权
def change_corn_default(details, lv, path, lines, words):
    info = {
        "details": details,
        "lv": lv,
        "path": path,
        "lines": lines,
        "words": words,
        "time": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    }
    collection5.insert_one(info)  # 写入mongodb
    print(info, "警告信息已入数据库")

